package com.kmproject.haifu.mvp.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.widget.autolayout.AutoToolbar;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.utils.PwdCheckUtil;
import com.kmproject.haifu.di.component.DaggerUserSetingUpdatePwdComponent;
import com.kmproject.haifu.mvp.contract.UserSetingUpdatePwdContract;
import com.kmproject.haifu.mvp.presenter.UserSetingUpdatePwdPresenter;
import com.xw.repo.XEditText;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 06/24/2019 10:42
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
public class UserSetingUpdatePwdActivity extends BaseActivity<UserSetingUpdatePwdPresenter> implements UserSetingUpdatePwdContract.View {

    @BindView(R.id.toolbar_back)
    RelativeLayout toolbarBack;
    @BindView(R.id.toolbar_title)
    TextView toolbarTitle;
    @BindView(R.id.back_right_icon)
    ImageView backRightIcon;
    @BindView(R.id.back_right_txt)
    TextView backRightTxt;
    @BindView(R.id.toolbar_right)
    RelativeLayout toolbarRight;
    @BindView(R.id.toolbar)
    AutoToolbar toolbar;
    @BindView(R.id.userSet_update_ymm_xedt)
    XEditText userSetUpdateYmmXedt;
    @BindView(R.id.userSet_update_xmm_xedt)
    XEditText userSetUpdateXmmXedt;
    @BindView(R.id.userSet_update_qrmm_xedt)
    XEditText userSetUpdateQrmmXedt;
    @BindView(R.id.userSet_update_xgmm_txt)
    TextView userSetUpdateXgmmTxt;

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerUserSetingUpdatePwdComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .view(this)
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_user_seting_update_pwd; //如果你不需要框架帮你设置 setContentView(id) 需要自行设置,请返回 0
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {
        setListener();
        setBtnStatus();
    }

    public void setListener() {
        userSetUpdateYmmXedt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setBtnStatus();
            }
        });
        userSetUpdateXmmXedt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setBtnStatus();
            }
        });

        userSetUpdateQrmmXedt.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                setBtnStatus();
            }
        });
    }

    @Override
    public void showLoading() {
          loadingDialog.show();
    }

    @Override
    public void hideLoading() {
     loadingDialog.dismiss();
    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick(R.id.userSet_update_xgmm_txt)
    public void onClick() {
        if (!PwdCheckUtil.isLetterAndDigit(userSetUpdateXmmXedt.getText().toString()))
        {
            showMessage(ArmsUtils.getString(this,R.string.user_setpwd_tip));
            return;
        }
        if (!userSetUpdateXmmXedt.getText().toString().trim().equals(userSetUpdateQrmmXedt.getText().toString().trim()))
        {
            showMessage(ArmsUtils.getString(this,R.string.user_updatepwd_tip));
            return;
        }
         mPresenter.updPassword(userSetUpdateYmmXedt.getText().toString().trim(),userSetUpdateXmmXedt.getText().toString().trim());
    }

    /**
     * 设置登录按钮状态
     *
     * @param
     */
    public void setBtnStatus() {
        if (userSetUpdateYmmXedt.getText().toString().length() > 7 && userSetUpdateXmmXedt.getText().toString().length() > 7&&userSetUpdateQrmmXedt.getText().toString().trim().length()>7 )

        {
            userSetUpdateXgmmTxt.setEnabled(true);
            userSetUpdateXgmmTxt.setBackgroundResource(R.drawable.button_yellow_bg);
            userSetUpdateXgmmTxt.setTextColor(getResources().getColor(R.color.color_191919));
        } else {
            userSetUpdateXgmmTxt.setEnabled(false);
            userSetUpdateXgmmTxt.setBackgroundResource(R.drawable.button_gray_bg);
            userSetUpdateXgmmTxt.setTextColor(getResources().getColor(R.color.color_EDEDED));
        }
    }

    @Override
    public void updPasswordSuccess() {
        killMyself();
    }
}
